trigger trg_AmendmentTrigger on Amendment__c (before update,after update , after insert) 
{
    
    Map<Id,Id> accountIDMap =  new Map<Id,Id>();
    Map<Id,Id> accountIDMapForShareOwnership =  new Map<Id,Id>();
    Map<Id,Id> accountIDMapForCompanyName =  new Map<Id,Id>();
    Map<Id,Amendment__c> amendmentObjectMap = new Map<Id,Amendment__c>();
    Map<Id,Amendment__c> amendmentObjectMapForChangePercentage = new Map<Id,Amendment__c>();
    List<Receipt__c> receiptUpdateList = new List<Receipt__c>();
    Set<Id> amendmentIds = new Set<Id>();
    
    
    
    for(Amendment__c amendmentObject : Trigger.New)
    {
        if(trigger.isBefore && trigger.isUpdate)
        {
            //Update the Owner to that person who approved the records...
            if(amendmentObject.Record_Approver_Id__c != null || amendmentObject.Record_Approver_Id__c != '')
            {
            
                System.debug('In Record Approver Old Value => ' + Trigger.oldMap.get(amendmentObject.Id).Record_Approver_Id__c);
                if(Trigger.oldMap.get(amendmentObject.Id).Record_Approver_Id__c != amendmentObject.Record_Approver_Id__c)
                {
                    amendmentObject.ownerId = amendmentObject.Record_Approver_Id__c;
                    System.debug('In Record Approver Set Value ' + amendmentObject.ownerId);    
                }    
                
               
            }
            System.debug('The Amendment record Type => '+ amendmentObject.Record_Type_Name_Formula__c );    
        }
        
        if(trigger.isAfter && Trigger.isUpdate)
        {
            //For Amendment Record types Logics...
            if(amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_License_Type' || amendmentObject.Record_Type_Name_Formula__c == 'Location_Amendment_Remove_Location'|| amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Add_Partner' || amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Change_Share_Percentage' || amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_Company_Manager' || amendmentObject.Record_Type_Name_Formula__c == 'Location_Amendment_Add_Location' || amendmentObject.Record_Type_Name_Formula__c == 'Location_Amendment_Change_Location' || amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_Company_Activity' || amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_Company_Name' || amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Remove_Partner' || amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Replace_Partner'  || amendmentObject.Record_Type_Name_Formula__c == 'Location_Amendment_New_Key')
            {
                //Update the Location and the Account....
                if(amendmentObject.Amendment_Application_Status__c  == 'Submitted By Archive Department')
                {
                    if(Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c != amendmentObject.Amendment_Application_Status__c)
                    {
                        accountIDMap.put(amendmentObject.Id,amendmentObject.Account__c);        
                    }
                    
                }
                
                //For SLA Implementation...
                if(amendmentObject.Amendment_Application_Status__c  == 'Approved to Delivery Department')
                {
                    if(Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c != amendmentObject.Amendment_Application_Status__c)
                    {
                        //Adding in the set...
                        amendmentIds.add(amendmentObject.Id);        
                    }
                    
                }
                
                
            }
            
            //For 'Shareholder_Amendment_Replace_Partner' && 'Shareholder_Amendment_Remove_Partner' Business Logic...
            if(amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Replace_Partner' || amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Remove_Partner')
            {
                System.debug('The previous Value of The Amendment Status => ' + Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c + ' && The Amendment Application Status ==> '+amendmentObject.Amendment_Application_Status__c ); 
                if(amendmentObject.Amendment_Application_Status__c  == 'Ready for Processing Chamber Certificate')
                {
                    System.debug('The previous Value of The Amendment Status => ' + Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c + ' && The Amendment Application Status ==> '+amendmentObject.Amendment_Application_Status__c ); 
                    if(Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c != amendmentObject.Amendment_Application_Status__c)
                    {
                        accountIDMapForShareOwnership.put(amendmentObject.Id,amendmentObject.Account__c);    
                        
                    }
                    
                }    
            
            }
            
            //For Company Name...
            if(amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_Company_Name')
            {
                System.debug('The previous Value of The Amendment Status => ' + Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c + ' && The Amendment Application Status ==> '+amendmentObject.Amendment_Application_Status__c ); 
                if(amendmentObject.Amendment_Application_Status__c  == 'Ready for Processing Chamber Certificate')
                {
                    if(Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c != amendmentObject.Amendment_Application_Status__c)
                    {
                        accountIDMapForCompanyName.put(amendmentObject.Id,amendmentObject.Account__c);  
                    }
                    
                }
            
            }   
        }    
        
        
    }
   
    //Now the Logic Begins...
    try
    {
        //For Company Name...
        if(accountIDMapForCompanyName.size() > 0)
        {
            List<Account> updateAccountList = new List<Account>();
            Map<ID,Account> accountObjectMap = new Map<ID,Account>([SELECT id,Name, LegalStatus__c,Authorized_Manager__c,company_name_arabic__c,(Select Id,Company__c,Shareholder__r.Passport_Number_Formula__c,Shareholder__r.Gender__c,Shareholder__r.Arabic_Name__c, Shareholder__c, Ownership__c,Share_Profit__c From Share_Ownerships__r) ,(SELECT Account__c,Activity__c FROM Account_Activities__r)  FROM Account where Id IN: accountIDMapForCompanyName.values()]);
            
            for(Account accountObject : [SELECT id,Name, LegalStatus__c,Authorized_Manager__c,company_name_arabic__c FROM Account where Id IN: accountIDMapForCompanyName.values()])
            {
                for(Amendment__c amendmentObject : Trigger.New)
                {
                    //Change the Account Name And Account Arabic Name...
                    if(amendmentObject.Account__c == accountObject.Id)
                    {
                        accountObject.Name = amendmentObject.New_Company_Name__c;
                        accountObject.company_name_arabic__c = amendmentObject.New_Company_Name_Arabic__c;
                        
                        //Adding in the Account update list...
                        updateAccountList.add(accountObject);
                        break;
                       
                    }   
                }
            }
            
            //Now update the Account...
            if(updateAccountList.size() > 0)
                update updateAccountList;
        
        }
        //For Record Type  Shareholder Replace and Remove Business Logic...
        if(accountIDMapForShareOwnership.size() > 0)
        {
            Map<ID,Account> accountObjectMap = new Map<ID,Account>([SELECT id,Name, LegalStatus__c,Authorized_Manager__c,company_name_arabic__c,(Select Id,Company__c,Shareholder__r.Passport_Number_Formula__c,Shareholder__r.Gender__c,Shareholder__r.Arabic_Name__c, Shareholder__c, Ownership__c,Share_Profit__c From Share_Ownerships__r) ,(SELECT Account__c,Activity__c FROM Account_Activities__r)  FROM Account where Id IN: accountIDMapForShareOwnership.values()]);
            List<Share_Ownership__c> shareOwnershipDeleteList = new List<Share_Ownership__c>();
            List<Share_Ownership__c> shareOwnershipInsertList = new List<Share_Ownership__c>();
            List<Share_Ownership__c> shareOwnershipEditList = new List<Share_Ownership__c>();
            Map<String,ID> legalStatusMap = new Map<String,ID>();
            List<Account> accountObjectUpdateList = new List<Account>();
            //For the Legal Status...
            for(Legal_Status__c legalStatus : [SELECT Id,Short_Name_English__c from Legal_Status__c where Short_Name_English__c =: 'F.Z.E' OR Short_Name_English__c =: 'F.Z.C'])
            {
                legalStatusMap.put(legalStatus.Short_Name_English__c,legalStatus.id);
            
            }
            
            for(Amendment__c amendmentObject : [select Id,Total_Share_Holders__c,Total_Share_Ownership__c,New_Activity_1__c,Action__c,New_Authorize_Manager__c,New_Activity_2__c,New_Activity_3__c,Current_Activity_1__c,Current_Activity_2__c,Current_Activity_3__c, Record_Type_Name_Formula__c, Chamber_History__c, New_Company_Name__c,New_Company_Name_Arabic__c, Account__c,(SELECT Id,Add_Flag__c,Edit_Flag__c,Delete_Flag__c,NAME,Contact__r.Passport_Number_Formula__c,Account__c,Action__c,Contact__r.Arabic_Name__c,Contact__r.Gender__c,Contact__c,Ownership__c,Share_Profit__c,Share_Ownership__c,ShareHolder_Name__c FROM Shareholder_Amendments__r) FROM Amendment__c where Id IN: accountIDMapForShareOwnership.keyset() ])
            {
                if(amendmentObject.Total_Share_Holders__c > 1)
                {
                    accountObjectMap.get(amendmentObject.Account__c).LegalStatus__c =  legalStatusMap.get('F.Z.C');           
                    //Adding in the Account update list...
                    accountObjectUpdateList.add(accountObjectMap.get(amendmentObject.Account__c));
                }
                else
                {
                    accountObjectMap.get(amendmentObject.Account__c).LegalStatus__c =  legalStatusMap.get('F.Z.E'); 
                    //Adding in the Account update list...
                    accountObjectUpdateList.add(accountObjectMap.get(amendmentObject.Account__c));
                }
                //Change the Account Name And Account Arabic Name...
                if(amendmentObject.Account__c == accountObjectMap.get(amendmentObject.Account__c).Id)
                {
                    //For Share Holder...
                    for(Shareholder_Amendment__c shareAmendmentObject : amendmentObject.Shareholder_Amendments__r)
                    {
                        //if(shareAmendmentObject.Action__c == 'Add')
                        if(shareAmendmentObject.Add_Flag__c == true)
                        {
                             if(shareAmendmentObject.Share_Ownership__c == null)
                             {
                                 System.debug('Under the Share Ownershipt => '+ shareAmendmentObject.Share_Ownership__c);
                                 Share_Ownership__c tempShareOwnerObject = new Share_Ownership__c();
                                 tempShareOwnerObject.Company__c = accountObjectMap.get(amendmentObject.Account__c).Id;
                                 tempShareOwnerObject.Ownership__c = shareAmendmentObject.Ownership__c;
                                 tempShareOwnerObject.Share_Profit__c = shareAmendmentObject.Share_Profit__c;
                                 tempShareOwnerObject.Shareholder__c = shareAmendmentObject.Contact__c;    
                                 
                                 //Adding in the List...
                                 shareOwnershipInsertList.add(tempShareOwnerObject);
                             
                             }
                                    
                        }
                        //else if(shareAmendmentObject.Action__c == 'Edit' || shareAmendmentObject.Action__c == 'Replace' || shareAmendmentObject.Action__c == 'Remove')  
                        else if(shareAmendmentObject.Edit_Flag__c == true || shareAmendmentObject.Delete_Flag__c == true)  
                        {
                            for(Share_Ownership__c shareOwnershipObject : accountObjectMap.get(amendmentObject.Account__c).Share_Ownerships__r)
                            {
                                if(shareAmendmentObject.Share_Ownership__c == shareOwnershipObject.Id)
                                {
                                    //if(shareAmendmentObject.Action__c == 'Remove')
                                    if(shareAmendmentObject.Delete_Flag__c == true)
                                    {
                                       shareOwnershipDeleteList.add(shareOwnershipObject);
                                    }
                                    //else if(shareAmendmentObject.Action__c == 'Edit' || shareAmendmentObject.Action__c == 'Replace')
                                    else if(shareAmendmentObject.Edit_Flag__c == true)
                                    {
                                        shareOwnershipObject.Ownership__c = shareAmendmentObject.Ownership__c;
                                        shareOwnershipObject.Share_Profit__c = shareAmendmentObject.Share_Profit__c;
                                        shareOwnershipObject.Shareholder__c = shareAmendmentObject.Contact__c;
                                        
                                        //Adding in the List...
                                        shareOwnershipEditList.add(shareOwnershipObject);
                                    }
                                    
                                    //Break the Loop...
                                    break;
                                }
                            
                            }
                        
                        }
                      
                    }
                }
             
             
                //Now Update the Amendment Associated Receipt to make sure after this step user can't cancell this Receipt...(11/Feb/2013)
                for(Receipt__c receiptObject : [SELECT id, isProcessConfirmed__c, CreatedDate,is_Cancelled_Receipt__c  FROM Receipt__c where Amendment__c IN: accountIDMapForShareOwnership.keyset() AND is_Cancelled_Receipt__c = false AND isProcessConfirmed__c =: false])
                {
                    receiptObject.isProcessConfirmed__c = true;
                    //Adding in the List...
                    receiptUpdateList.add(receiptObject);   
                }
                
             }
         
            //For Share Ownershipp....
            if(shareOwnershipInsertList.size() > 0)
               insert shareOwnershipInsertList;
            if(shareOwnershipEditList.size() > 0)
               update shareOwnershipEditList;
            if(shareOwnershipDeleteList.size() > 0)
               delete shareOwnershipDeleteList;     
            if(accountObjectUpdateList.size() > 0)
                update accountObjectUpdateList;    
            
            //Now Update the Amendment Receipt...
            if(receiptUpdateList.size() > 0)
                update receiptUpdateList;
        
        }
        
        //For Archiving the Data Business Logic...
        if(accountIDMap.size() > 0)
        {
            List<Account> updateAccountList = new List<Account>();
            
            RecordType accountRecordTypes = [select Id,Name,DeveloperName,SObjectType,IsActive From RecordType  where DeveloperName =: 'Registered' AND IsActive =: true AND SObjectType =: 'Account' LIMIT 1];
            
            //For Account....
            For(Account accountObject : [SELECT id,RecordTypeId,License_Application_Step__c FROM Account where Id IN: accountIDMap.values()])
            {
                accountObject.License_Application_Step__c = 'Archived';
                accountObject.RecordTypeId = accountRecordTypes.Id ;
                
                //Adding in the List...
                updateAccountList.add(accountObject);
            }
            
            //Now Update the Account List...
            if(updateAccountList.size() > 0)    
                update updateAccountList;
        
        }    
    }
    catch (Exception exp)
    {
        
        throw exp;
    }
    
    
    //For SLA Implementation.... (SLA Record Creation)
    if(Trigger.isInsert && Trigger.isAfter)
    {
        Map<string,integer> amendmentCustomSettingMap = new Map<string,integer>();
        List<SLA__c> slaList = new List<SLA__c>();
        
        for(SLA_Custom_Setting__c slaRecord : SLA_Custom_Setting__c.getAll().values())
        {
            if(slaRecord.Process_Type__c == 'Amendment')
            {
                amendmentCustomSettingMap.put(slaRecord.Application_Type__c , integer.valueof(slaRecord.SLA_Hours__c));
            }
        }
        
        if(amendmentCustomSettingMap.size() > 0)
        {
            // Assumes the BSC work hours are the default for the Org
            List<BusinessHours> businessHoursObjectList = new List<BusinessHours>([select id from businesshours where isDefault = true LIMIT 1]) ;

            if(businessHoursObjectList.size() > 0)
            {    
                for(Amendment__c amendmentObject : Trigger.New)
                {
                    if(amendmentCustomSettingMap.get(amendmentObject.Record_Type_Name_Formula__c) != null) 
                    {
                        SLA__c slaObject = new SLA__c();
                        
                        slaObject.SLA_Hours__c = amendmentCustomSettingMap.get(amendmentObject.Record_Type_Name_Formula__c) ;
                        slaObject.Actual_Start_Date_Time__c = DateTime.Now();
                        
                        // BSC works 11.5 hours / day (8:00 AM - 7:30 PM, M-F). Our SLA is 2-days (23 business hours)
                        System.debug('The Actual Start Time => '+ slaObject.Actual_Start_Date_Time__c );
                        System.debug('The Expected End Date => '+ slaObject.Actual_Start_Date_Time__c.addhours(amendmentCustomSettingMap.get(amendmentObject.Record_Type_Name_Formula__c)));
                        System.debug('The Business Hour Date => ' + BusinessHours.add(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, amendmentCustomSettingMap.get(amendmentObject.Record_Type_Name_Formula__c)* 60 * 60 * 1000L));
                        System.debug('The Business Hour Date GMT => ' + BusinessHours.addGmt(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, amendmentCustomSettingMap.get(amendmentObject.Record_Type_Name_Formula__c) * 60 * 60 * 1000L));
                        slaObject.Expected_End_Date__c = BusinessHours.addGmt(businessHoursObjectList[0].id, slaObject.Actual_Start_Date_Time__c, amendmentCustomSettingMap.get(amendmentObject.Record_Type_Name_Formula__c) * 60 * 60 * 1000L);
                        
                        //slaObject.Expected_End_Date__c = DateTime.Now().addhours(amendmentCustomSettingMap.get(amendmentObject.Record_Type_Name_Formula__c) );
                        slaObject.License__c = amendmentObject.Account__c;
                        slaObject.Amendment__c = amendmentObject.Id ;
                        slaObject.Process_Type__c = 'Amendment';
                        slaObject.Application_Type__c  = amendmentObject.Record_Type_Name__c;
                        //Adding in the List...
                        slaList.add(slaObject);           
                   }
                }
                
                //Now Upsert the List...
                if(slaList.size() > 0)
                    upsert slaList;
            }
        }    
    }
    
    //For SLA Implementaton (Record Update )...
    if(amendmentIds.size() > 0)
    {
        List<SLA__c> slaList = new List<SLA__c>();
       
        for(SLA__c slaObject : [SELECT ID, isCompleted__c, Actual_End_Date_Time__c, Actual_Start_Date_Time__c, Amendment__c, Application_Type__c, Expected_End_Date__c, License__c, Process_Type__c, SLA_Hours__c, Status__c, Tenancy_Contract__c,isCancelled__c FROM SLA__c where Amendment__c IN: amendmentIds AND isCompleted__c =: false AND isCancelled__c =: false])
        {
        
            //Putting the Actual_End_Date_Time__c End Date of SLA...
            slaObject.Actual_End_Date_Time__c = DateTime.Now();
            slaObject.isCompleted__c =true;
            //Adding in the List...
            slaList.add(slaObject);
        }
        
        //Upserting the SLA List...
        if(slaList.size() > 0)
            upsert slaList;
        
    }
        
    //Previous Approach before 3rd July 2012....(When we were not using the Confirm Button...)
    /*Map<ID,ID> locationIDMap = new Map<ID,ID>();
    Map<Id,Id> accountIDMap =  new Map<Id,Id>();
    Map<Id,Id> accountIDMapForChangePercentage =  new Map<Id,Id>();
    Map<Id,Amendment__c> amendmentObjectMap = new Map<Id,Amendment__c>();
    Map<Id,Amendment__c> amendmentObjectMapForChangePercentage = new Map<Id,Amendment__c>();
    
    for(Amendment__c amendmentObject : Trigger.New)
    {
        //Update the Owner to that person who approved the records...
        if(amendmentObject.Record_Approver_Id__c != null || amendmentObject.Record_Approver_Id__c != '')
        {
        
            System.debug('In Record Approver Old Value => ' + Trigger.oldMap.get(amendmentObject.Id).Record_Approver_Id__c);
            if(Trigger.oldMap.get(amendmentObject.Id).Record_Approver_Id__c != amendmentObject.Record_Approver_Id__c)
            {
                amendmentObject.ownerId = amendmentObject.Record_Approver_Id__c;
                System.debug('In Record Approver Set Value ' + amendmentObject.ownerId);    
            }    
            
           
        }
        System.debug('The Amendment record Type => '+ amendmentObject.Record_Type_Name_Formula__c );
        
        //For 'Shareholder_Amendment_Change_Share_Percentage' Business Logic...
        if(amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Change_Share_Percentage')
        {
            System.debug('The previous Value of The Amendment Status => ' + Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c + ' && The Amendment Application Status ==> '+amendmentObject.Amendment_Application_Status__c ); 
            if(amendmentObject.Amendment_Application_Status__c  == 'Approved By BackOffice Department')
            {
                System.debug('The previous Value of The Amendment Status => ' + Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c + ' && The Amendment Application Status ==> '+amendmentObject.Amendment_Application_Status__c ); 
                if(Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c != amendmentObject.Amendment_Application_Status__c)
                {
                    accountIDMapForChangePercentage.put(amendmentObject.Id,amendmentObject.Account__c);    
                    //amendmentObjectMapForChangePercentage.put(amendmentObject.Id,amendmentObject);
                }
                
            }    
        
        }
        
        
        
        //For Amendment Record types Logics...
        if(amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_License_Type' || amendmentObject.Record_Type_Name_Formula__c == 'Location_Amendment_Remove_Location'|| amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Add_Partner' || amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Change_Share_Percentage' || amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_Company_Manager' || amendmentObject.Record_Type_Name_Formula__c == 'Location_Amendment_Add_Location' || amendmentObject.Record_Type_Name_Formula__c == 'Location_Amendment_Change_Location' || amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_Company_Activity' || amendmentObject.Record_Type_Name_Formula__c == 'Company_Amendment_Change_Company_Name' || amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Remove_Partner' || amendmentObject.Record_Type_Name_Formula__c == 'Shareholder_Amendment_Replace_Partner')
        {
            //Update the Location and the Account....
            if(amendmentObject.Amendment_Application_Status__c  == 'Submitted By Archive Department')
            {
                if(Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c != amendmentObject.Amendment_Application_Status__c)
                {
                    accountIDMap.put(amendmentObject.Id,amendmentObject.Account__c);        
                }
                
            }
            
       }         
         
        //For Amendment Location Key Record Type Logic...
        if(amendmentObject.Record_Type_Name_Formula__c == 'Location_Amendment_New_Key')
        {
            //Update the Location and the Account....
            if(amendmentObject.Amendment_Application_Status__c  == 'Submitted By Archive Department')
            {
                if(Trigger.oldMap.get(amendmentObject.Id).Amendment_Application_Status__c != amendmentObject.Amendment_Application_Status__c)
                {
                    accountIDMap.put(amendmentObject.Id,amendmentObject.Account__c);        
                    locationIDMap.put(amendmentObject.Id,amendmentObject.Location_ForKey__c);
                    amendmentObjectMap.put(amendmentObject.Location_ForKey__c,amendmentObject);
                    
                }    
               
            }
    
        }
        
        
    
    }
    try
    {
        //For Record Type  Shareholder_Amendment_Change_Share_Percentage Business Logic...
        if(accountIDMapForChangePercentage.size() > 0)
        {
            Map<ID,Account> accountObjectMap = new Map<ID,Account>([SELECT id,Name, LegalStatus__c,Authorized_Manager__c,company_name_arabic__c,(Select Id,Company__c,Shareholder__r.Passport_Number_Formula__c,Shareholder__r.Gender__c,Shareholder__r.Arabic_Name__c, Shareholder__c, Ownership__c,Share_Profit__c From Share_Ownerships__r) ,(SELECT Account__c,Activity__c FROM Account_Activities__r)  FROM Account where Id IN: accountIDMapForChangePercentage.values()]);
            List<Share_Ownership__c> shareOwnershipDeleteList = new List<Share_Ownership__c>();
            List<Share_Ownership__c> shareOwnershipInsertList = new List<Share_Ownership__c>();
            List<Share_Ownership__c> shareOwnershipEditList = new List<Share_Ownership__c>();
            
             for(Amendment__c amendmentObject : [select Id,Total_Share_Ownership__c,New_Activity_1__c,Action__c,New_Authorize_Manager__c,New_Activity_2__c,New_Activity_3__c,Current_Activity_1__c,Current_Activity_2__c,Current_Activity_3__c, Record_Type_Name_Formula__c, Chamber_History__c, New_Company_Name__c,New_Company_Name_Arabic__c, Account__c,(SELECT Id,Add_Flag__c,Edit_Flag__c,Delete_Flag__c,NAME,Contact__r.Passport_Number_Formula__c,Account__c,Action__c,Contact__r.Arabic_Name__c,Contact__r.Gender__c,Contact__c,Ownership__c,Share_Profit__c,Share_Ownership__c,ShareHolder_Name__c FROM Shareholder_Amendments__r) FROM Amendment__c where Id IN: accountIDMapForChangePercentage.keyset() ])
             {
                 //Change the Account Name And Account Arabic Name...
                if(amendmentObject.Account__c == accountObjectMap.get(amendmentObject.Account__c).Id)
                {
                    //For Share Holder...
                    for(Shareholder_Amendment__c shareAmendmentObject : amendmentObject.Shareholder_Amendments__r)
                    {
                        //if(shareAmendmentObject.Action__c == 'Add')
                        if(shareAmendmentObject.Add_Flag__c == true)
                        {
                             if(shareAmendmentObject.Share_Ownership__c == null)
                             {
                                 System.debug('Under the Share Ownershipt => '+ shareAmendmentObject.Share_Ownership__c);
                                 Share_Ownership__c tempShareOwnerObject = new Share_Ownership__c();
                                 tempShareOwnerObject.Company__c = accountObjectMap.get(amendmentObject.Account__c).Id;
                                 tempShareOwnerObject.Ownership__c = shareAmendmentObject.Ownership__c;
                                 tempShareOwnerObject.Share_Profit__c = shareAmendmentObject.Share_Profit__c;
                                 tempShareOwnerObject.Shareholder__c = shareAmendmentObject.Contact__c;    
                                 
                                 //Adding in the List...
                                 shareOwnershipInsertList.add(tempShareOwnerObject);
                             
                             }
                                    
                        }
                        //else if(shareAmendmentObject.Action__c == 'Edit' || shareAmendmentObject.Action__c == 'Replace' || shareAmendmentObject.Action__c == 'Remove')  
                        else if(shareAmendmentObject.Edit_Flag__c == true || shareAmendmentObject.Delete_Flag__c == true)  
                        {
                            for(Share_Ownership__c shareOwnershipObject : accountObjectMap.get(amendmentObject.Account__c).Share_Ownerships__r)
                            {
                                if(shareAmendmentObject.Share_Ownership__c == shareOwnershipObject.Id)
                                {
                                    //if(shareAmendmentObject.Action__c == 'Remove')
                                    if(shareAmendmentObject.Delete_Flag__c == true)
                                    {
                                       shareOwnershipDeleteList.add(shareOwnershipObject);
                                    }
                                    //else if(shareAmendmentObject.Action__c == 'Edit' || shareAmendmentObject.Action__c == 'Replace')
                                    else if(shareAmendmentObject.Edit_Flag__c == true)
                                    {
                                        shareOwnershipObject.Ownership__c = shareAmendmentObject.Ownership__c;
                                        shareOwnershipObject.Share_Profit__c = shareAmendmentObject.Share_Profit__c;
                                        shareOwnershipObject.Shareholder__c = shareAmendmentObject.Contact__c;
                                        
                                        //Adding in the List...
                                        shareOwnershipEditList.add(shareOwnershipObject);
                                    }
                                    
                                    //Break the Loop...
                                    break;
                                }
                            
                            }
                        
                        }
                        
                    
                    }
                }
             }
         
            //For Share Ownershipp....
            if(shareOwnershipInsertList.size() > 0)
               insert shareOwnershipInsertList;
            if(shareOwnershipEditList.size() > 0)
               update shareOwnershipEditList;
            if(shareOwnershipDeleteList.size() > 0)
               delete shareOwnershipDeleteList;     
                
        
        }
        
        //For Archiving the Data and Location For Key Business Logic...
        if(accountIDMap.size() > 0)
        {
            List<Account> updateAccountList = new List<Account>();
            List<Location__c> updateLocationList = new List<Location__c>();
            
            RecordType accountRecordTypes = [select Id,Name,DeveloperName,SObjectType,IsActive From RecordType  where DeveloperName =: 'Registered' AND IsActive =: true AND SObjectType =: 'Account' LIMIT 1];
            
            //For Account....
            For(Account accountObject : [SELECT id,RecordTypeId,License_Application_Step__c FROM Account where Id IN: accountIDMap.values()])
            {
                accountObject.License_Application_Step__c = 'Archived';
                accountObject.RecordTypeId = accountRecordTypes.Id ;
                
                //Adding in the List...
                updateAccountList.add(accountObject);
            }
            
            //For Locations...
            For(Location__c locationObject : [select id,Number_of_Keys__c from Location__c where Id IN: locationIDMap.values()])
            {
                System.debug('The Location Object => ' + locationObject.Id + ' && The Location Key => '+locationObject.Number_of_Keys__c);
                if(locationObject.Number_of_Keys__c != null || locationObject.Number_of_Keys__c != '' || amendmentObjectMap.get(locationObject.id).Keys_Required__c != null || amendmentObjectMap.get(locationObject.id).Keys_Required__c != '')
                    locationObject.Number_of_Keys__c = String.valueof(integer.valueof(locationObject.Number_of_Keys__c) + integer.valueof(amendmentObjectMap.get(locationObject.id).Keys_Required__c));
            
                // ||
                //Adding in the List...
                updateLocationList.add(locationObject);
            } 
            
            //Now Update Account and Locations Keys...
            if(updateLocationList.size() > 0 )
                update updateLocationList;
                
            if(updateAccountList.size() > 0)    
                update updateAccountList;
            
            
        
        }   
        
    }
    catch (Exception exp)
    {
        
        throw exp;
    }
    */
    
}